package com.bbcloud.controller.portal;

import com.bbcloud.common.ServerResponse;
import com.bbcloud.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by liuzhu on 2018/12/11.
 */
@Controller
@RequestMapping("/shop/")
public class ShopController {

    @Autowired
    private IShopService iShopService;

    /**
     * 获取所有商品列表
     * @param pageNum   页号
     * @param pageSize  每页几条
     * @param orderBy   排序
     * @return          所有商品列表
     */
    @RequestMapping(value = "list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               @RequestParam(value = "orderBy", defaultValue = "") java.lang.String orderBy)
    {
        return iShopService.list(pageNum,pageSize,orderBy);
    }


    /**
     * 添加到购物车
     * @param token token
     * @param userId    userId
     * @param productId 商品Id
     * @param quantity  数量
     * @return  添加到购物车是否成功
     */
    @RequestMapping(value = "add_to_cart.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addToCart(@RequestParam(value = "token", required = true) String token,
                                    @RequestParam(value = "userId", required = true) String userId,
                                    @RequestParam(value = "productId", required = true) String productId,
                                    @RequestParam(value = "quantity", defaultValue = "1") int quantity)
    {
        return iShopService.addToCart(token,userId,productId,quantity);
    }

    /**
     * 返回购物车中的产品列表
     * @param token     token
     * @param userId    userId
     * @param pageNum   页号
     * @param pageSize  每页大小
     * @param orderBy   排序
     * @return  购物车中的产品列表
     */
    @RequestMapping(value = "cart_product_list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse cartProductList(@RequestParam(value = "token", required = true) String token,
                                          @RequestParam(value = "userId", required = true) String userId,
                                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                          @RequestParam(value = "orderBy", defaultValue = "") java.lang.String orderBy)
    {
        return iShopService.cartProductList(token,userId,pageNum,pageSize,orderBy);
    }
}

